package com.itcast.yygh.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.common.result.R;
import com.itcast.common.utils.AuthContextHolder;
import com.itcast.yygh.model.enums.OrderStatusEnum;
import com.itcast.yygh.model.model.order.OrderInfo;
import com.itcast.yygh.model.vo.order.OrderCountQueryVo;
import com.itcast.yygh.model.vo.order.OrderQueryVo;
import com.itcast.yygh.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
@RequiredArgsConstructor
public class OrderApiController {

    private final OrderService orderService;

    @ApiOperation("创建订单")
    @GetMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "就诊人id", required = true)
            @PathVariable Long patientId) {

        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return R.ok().data("orderId", orderId);
    }

    //订单列表（条件查询带分页）
    @ApiOperation("分页条件查询订单列表")
    @GetMapping("auth/{page}/{limit}")
    public R list(@PathVariable Long page,
                  @PathVariable Long limit,
                  OrderQueryVo orderQueryVo, HttpServletRequest request) {
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel =
                orderService.selectPage(pageParam, orderQueryVo);
        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("获取订单状态")
    @GetMapping("auth/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }

    //根据订单id查询订单详情
    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public R getOrders(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }

    @ApiOperation("取消预约")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public R cancelOrder(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {

        try {
            Boolean flag = orderService.cancelOrder(orderId);
            return R.ok().data("flag",flag);
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return R.error().message("嘻嘻，作者的商家证书过期了，不能退款哦~~~");
        }
    }

    @ApiOperation("获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}